package com.zyc.app.woa.utils;

import android.content.Context;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zyc on 12/28/14.
 */
public class Caches {
    private static HashMap<CacheKey, Object> sMap = new HashMap<CacheKey, Object>();

    public static void init(Context context) {
        SettingHelper.init(context);

        Map<String, ?> map = SettingHelper.getAll();
        CacheKey cacheKey;
        for (String key : map.keySet()) {
            cacheKey = CacheKey.valueOf(key);
            if (cacheKey.isPersistent()) {
                sMap.put(cacheKey, map.get(key));
            }
        }
    }


    public static void put(CacheKey key, Object obj) {
        sMap.put(key, obj);

        if (key.isPersistent()) {
            SettingHelper.put(key, obj);
        }
    }

    public static Object get(CacheKey key) {
        return sMap.get(key);
    }

    public static boolean getBoolean(CacheKey key, boolean defValue) {
        Object obj = sMap.get(key);
        return null != obj ? (Boolean)obj : defValue;
    }


    public enum CacheKey {
        READING_ARTICLE,
        CLEAR_AFTER_READ(true);


        private boolean mPersistent;

        CacheKey() {
        }

        CacheKey(boolean persistent) {
            mPersistent = persistent;
        }

        public boolean isPersistent() {
            return mPersistent;
        }
    }
}
